package com.wuhanins.insmytestdemo.app.launch;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.wuhanins.insmytestdemo.BuildConfig;
import com.wuhanins.insmytestdemo.R;
import com.wuhanins.insmytestdemo.app.activity.TestMainFragment;
import com.wuhanins.insmytestdemo.base.BaseActivity;
import com.wuhanins.zero.base.ZeroAutoPkgFragment;
import com.wuhanins.zero2.databinding.Insv2ActivityLauncherBinding;
import com.wuhanins.zero2.view.InsToastUtil;
import com.wuhanins.zero2.view.main.HostCustomerBackProgressEvents;
import com.wuhanins.zero2.view.main.OnCustomerBackProgressEvents;

import java.util.Map;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

import static androidx.fragment.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN;

/**
 * Author  ： logan
 * Time    ： 10/12/21 3:30 PM
 * Desc    ：
 */
@RuntimePermissions
public class LauncherActivity extends BaseActivity<Insv2ActivityLauncherBinding>
        implements ZeroAutoPkgFragment.OnShowFragmentEventListener, HostCustomerBackProgressEvents {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(Insv2ActivityLauncherBinding.class);
    }

    @Override
    protected void iView() {
//        ImmersionBar.with(this).init();
        LauncherActivityPermissionsDispatcher.initAfterPermissionCheckedWithPermissionCheck(this);
    }

    @Override
    protected void iData() {

    }

    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
    public void initAfterPermissionChecked() {

        Fragment f;
        if (BuildConfig.DEBUG) {
            f = new TestMainFragment();
        } else {
            f = new SplashFragment();
        }
        this._replaceFragment(R.id.liLauncherContainer, f);
    }

    @OnShowRationale({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
    //给用户解释要请求什么权限，为什么需要此权限
    public void showRationale(final PermissionRequest request) {
        new AlertDialog.Builder(LauncherActivity.this, R.style.Theme_AppCompat_Light_Dialog_Alert)
                .setMessage("使用APP需要存储权限，是否继续请求权限")
                .setPositiveButton("继续", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();//继续执行请求
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                request.cancel();//取消执行请求
            }
        })
                .show();
    }

    @OnNeverAskAgain({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
    public void multiNeverAsk() {
        InsToastUtil.showInBottom("权限未授予,部分功能可能无法正常执行");
        initAfterPermissionChecked();
    }

    //一旦用户拒绝了
    @OnPermissionDenied({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
    public void multiDenied() {
        InsToastUtil.showInBottom("已拒绝存储权限,将无法正常存储数据");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        LauncherActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    public void showFragment(Fragment fragment) {
        this.getSupportFragmentManager()
                .beginTransaction()
                .setTransition(TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(fragment.getClass().getSimpleName())
                .add(R.id.liLauncherContainer, fragment)
                .hide(getSupportFragmentManager().findFragmentById(R.id.liLauncherContainer))
                .commit();
    }

    @Override
    public void onBackPressed() {
        if (onCustomerBackProgressEvents != null && !onCustomerBackProgressEvents.keySet().isEmpty()) {
            boolean out = true;
            for (String tag : onCustomerBackProgressEvents.keySet()) {
                if (onCustomerBackProgressEvents.get(tag) != null) {
                    out = out && onCustomerBackProgressEvents.get(tag).onCustomerBackProgress();
                }
            }

            if (!out) {
                return;
            }
        }
        super.onBackPressed();
    }

    protected Map<String, OnCustomerBackProgressEvents> onCustomerBackProgressEvents;

    @Override
    public void putCustomerBackProgressEvent(String tag, OnCustomerBackProgressEvents e) {
        onCustomerBackProgressEvents.put(tag, e);
    }

}
